import * as Koa from "koa";
import * as logger from "koa-logger";
import * as json from "koa-json";
import * as bodyparser from "koa-bodyparser";
import * as views from "koa-views";
import * as koaStatic from "koa-static";
import * as path from "path";
import { router } from "./router";
import * as redis from "redis";
import * as bluebird from "bluebird";
import { init } from "./util/wechat";
const cors = require("koa-cors");
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

// init();

const app = new Koa();
app.use(logger());
app.use(cors());
app.use(json());
app.use(bodyparser({
    enableTypes: ['json', 'form', 'text']
}));
app.use(koaStatic(path.join(__dirname, '../static')));
app.use(views(path.join(__dirname, '../views'), { extension: 'ejs' }));
app.use(router.routes());
app.on('error', (err: Error, ctx: Koa.Context) => {
    console.log('server error:' + err.stack, ctx);
    ctx.status = 500;
});
app.listen(3000);